<?php

namespace App\Models;

use App\Models\Field\NoteField;

/**
 * App\Models\SysNote
 * This is the model class for table "sys_note_".
 *
 * @property int $id 自增长ID
 * @property string $note_id 笔记ID
 * @property string $user_id 用户ID
 * @property string $article_id 文章ID
 * @property int $type 笔记类型，1文章笔记，2文章划线笔记，3纯文字笔记，4带图的笔记
 * @property int $is_choice 是否被精选，0否，1是
 * @property int $is_deleted 删除状态，0正常，1删除
 * @property int $is_public 是否公开，0私密，1公开
 * @property int $is_shield 屏蔽状态，0正常，1屏蔽
 * @property int $created_at 创建时间戳
 * @property int $updated_at 更新时间戳
 *
 * @property SysArticle $article
 * @property SysNoteContent $content
 * @method static \Illuminate\Database\Eloquent\Builder|SysNote newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SysNote newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SysNote query()
 */
class SysNote extends BaseModel
{

    protected $table = "sys_note_";
    protected $primaryKey = "note_id";
    protected $keyType = "string";
    public $timestamps = true;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'created_at' => 'timestamp',
        'updated_at' => 'timestamp',
    ];

    /**
     * select columns
     * @var array
     */
    public $fields = [
        'content' => ['note_id', 'content'],
        'selected' => ['note_id', 'selected'],
        'article' => ['article_id', 'cover_url', 'title', 'description', 'is_deleted', 'is_shield', 'article_id as id'],
    ];

    /**
     * 关联模型 - 笔记内容
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function content()
    {
        return $this->hasOne(SysNoteContent::class, 'note_id', 'note_id', $this->note_id)
            ->select($this->fields['content']);
    }

    /**
     * 关联模型 - 选中内容
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function selected()
    {
        return $this->hasOne(SysNoteSelected::class, 'note_id', 'note_id', $this->note_id)
            ->select($this->fields['selected']);
    }

    /**
     * 模型属性 - 笔记内容
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getContentAttribute()
    {
        return SysNoteContent::getInstance($this->note_id, true)
            ->select($this->fields['content'])
            ->value('content');
    }

    /**
     * 模型属性 - 选中内容
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getSelectedAttribute()
    {
        return SysNoteSelected::getInstance($this->note_id, true)
            ->select($this->fields['selected'])
            ->value('selected');
    }

    /**
     * 模型属性 - 文章
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getArticleAttribute()
    {
        if (!$this->article_id) return null;
        return SysArticle::getInstance($this->article_id)
            ->where('article_id', $this->article_id)
            ->select($this->fields['article'])
            ->first();
    }

    public function commonFormat($scene = NoteField::BASE_INFO): array
    {
        $formats = NoteField::getFormats($scene);
        return NoteField::format($this, $formats);
    }
}
